import getExtraData from "./getExtraData";
const BASE_URL = 'http://localhost:3000/logs';
// const BASE_URL = 'http://124.70.190.167:3000/logs';
class Request {
    constructor(){
        this.url = BASE_URL;
        this.xhr = new XMLHttpRequest();
    }
    getIP(){
         fetch('/api/cityjson?ie=utf-8',{
            methods: 'GET',
        })
        .then(response => response.text())
        .then(data => {           
            localStorage.clear()
            window.localStorage.setItem('ipdata',data); 
        });

    }
    send(data = {}){

        return new Promise((resolve,reject) => {
            let extraData = getExtraData()??{};
            let log = {...extraData, ...data};
            console.log(log);
            // let body = JSON.stringify(log);
            // console.log(body)
            this.xhr.open('POST',this.url);//异步
            this.xhr.setRequestHeader('Content-Type','application/json') //发送json数据
            this.xhr.send(JSON.stringify({"data":log}));
            this.xhr.onreadystatechange = () => {
                if(this.xhr.readyState === 4 && this.xhr.status.toString().startsWith('2')){
                    // console.log(this.xhr.responseText)
                    resolve(this.xhr.responseText)       
                }
            }
          
        }) 
    }
}

export default new Request();